﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ECCIT;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;

public partial class EventInterface : System.Web.UI.Page
{
    private const string ELEMENT_FILE = "<file type=\"{0}\" url=\"{1}\" thumb=\"{2}\" errorPic=\"{3}\" errorThumbPic=\"{4}\"/>";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;

        string output = string.Empty;
        try
        {
            ResultInfo valRes = ValidateArguments();
            
            if (valRes.Result)
            {
                string sLangID = Request.Params["lang"];
                string sid = Request.Params["eid"];

                int langID = (string.IsNullOrEmpty(sLangID)) ? 2052 : int.Parse(sLangID);
                int? eid = int.Parse(sid);

                StringBuilder builder = new StringBuilder("<files>");
                if (eid > 0)
                {
                    EventsBLL bll = new EventsBLL(langID);
                    PagingList list = bll.GetUploadFilesByEventsID(eid, null);
                    for (int i = 0; i < list.Capacity; i++)
                    {
                        UploadFileModel ufm = (UploadFileModel)list.List[i];
                        if (ufm.FileType == 1)
                            builder.AppendFormat(RenderImage(ufm.Path, ufm.ThumbnailImagePath));
                        else if (ufm.FileType == 2)
                            builder.AppendFormat(RenderVideo(ufm.Path));
                        else if (ufm.FileType == 3)
                            builder.AppendFormat(RenderAudio(ufm.Path));
                    }
                }
                else
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (sid == "-1")
                            builder.AppendFormat(RenderImage("8fbec163-6740-44db-8eb6-7b06f4cdd52c.jpg", "d5b0c6c8-4c4d-4b04-8110-6c83c8de581f.jpg"));
                        else if (sid == "-2")
                            builder.AppendFormat(RenderVideo("32f33d05-ef4c-408b-b2cf-7ae29fa9f531.flv"));
                        else if (sid == "-3")
                            builder.AppendFormat(RenderAudio("c58d1906-7c92-4b0c-aad8-1128e70e3c96.mp3"));
                        builder.AppendFormat(RenderVideo("32f33d05-ef4c-408b-b2cf-7ae29fa9f531.flv"));
                        builder.AppendFormat(RenderAudio("c58d1906-7c92-4b0c-aad8-1128e70e3c96.mp3"));
                        builder.AppendFormat(RenderAudio("78c8915b-1b07-4861-9ee2-002114dd13e5.mp3"));
                        builder.AppendFormat(RenderVideo("7b567180-601c-40f9-86fd-cc1cea189193.flv"));
                        builder.AppendFormat(RenderImage("00221eaf-865e-4124-98af-a3e9226805fc.jpg", "b7180f1a-cfaa-4567-9be0-7ed3aec3d1db.jpg"));
                        builder.AppendFormat(RenderVideo("d6cc284d-2ea5-4b42-a83f-0f0e6d5933bf.flv"));
                        builder.AppendFormat(RenderAudio("7d380aa5-e9c3-4502-a123-d78f5f8347af.mp3"));
                    }
                }
                builder.Append("</files>");

                output = builder.ToString();
            }
        }
        catch (Exception ex)
        {
            output = "<files value=\"false\"></files>";
        }
        //output = "<result>0</result>";
        Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + output);
    }

    private ResultInfo ValidateArguments()
    {
        ResultInfo result = new ResultInfo(false);
        string sLangID = Request.Params["lang"];
        string sid = Request.Params["eid"];
        if (string.IsNullOrEmpty(sLangID)) return result;
        if (string.IsNullOrEmpty(sid)) return result;
        
        int langID;
        bool b = int.TryParse(sLangID, out langID);
        if (!b) return result;
        if (langID != 2052 && langID != 1033) return result;

        int eid;
        b = int.TryParse(sid, out eid);
        if (!b) return result;

        result.Result = true;
        return result;
    }

    private string RenderImage(string url, string thumbUrl)
    {
        return string.Format(ELEMENT_FILE, 1,
                            "/uploadfiles/image/" + url,
                            "/uploadfiles/image/" + thumbUrl,
                            "/cn/images/event_detail_temp_4.jpg",
                            "/cn/images/default_slide_img.jpg");
    }

    private string RenderVideo(string url)
    {
        return string.Format(ELEMENT_FILE, 2,
                            "/uploadfiles/media/" + url,
                            "/cn/images/default_video.jpg",
                            "/cn/images/event_detail_temp_2.jpg",
                            "/cn/images/default_video.jpg");
    }

    private string RenderAudio(string url)
    {
        return string.Format(ELEMENT_FILE, 3,
                        "/uploadfiles/media/" + url,
                        "/cn/images/default_audio.jpg",
                        "/cn/images/event_detail_temp_3.jpg",
                        "/cn/images/default_audio.jpg");
    }
}